package com.mit.service.design.dao.impl;

import java.util.HashMap;
import java.util.List;

import com.mit.common.design.dto.TemplateDTO;
import com.mit.common.exception.NoRecordUpdateException;
import com.mit.service.core.dao.AbstractDAO;
import com.mit.service.design.dao.ITemplateDAO;

public class TemplateDAOImpl extends AbstractDAO implements ITemplateDAO {

	@Override
	public TemplateDTO createTemplate(TemplateDTO to) {
		int id = (Integer) this.getSqlMapClientTemplate().insert("insertTemplate", to);
		to.setId(id);
		return to;
	}

	@Override
	public TemplateDTO retrieveTemplate(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public TemplateDTO updateTemplate(TemplateDTO to)
			throws NoRecordUpdateException {
		int i =  getSqlMapClientTemplate().update("updateTemplate", to);
		if(i < 1)
			throw new NoRecordUpdateException();
		return to;
	}

	@Override
	public void deleteTemplate(Integer id) {
		getSqlMapClientTemplate().delete("deleteTemplate", id);
		
	}

	@Override
	public List<TemplateDTO> retrieveTemplates(Integer styleId,
			String templateType) {
		HashMap param = new HashMap();
		param.put("styleId", styleId);
		param.put("type", templateType);
		return (List<TemplateDTO>) getSqlMapClientTemplate().queryForList("selectTemplatesByStyleAndType", param);
	}

	@Override
	public List<TemplateDTO> retrieveTemplatesByStyle(
			Integer styleId, String state, Integer excludedId) {
		HashMap param = new HashMap();
		param.put("state", state);
		param.put("styleId", styleId);
		param.put("excludedId", excludedId);
		return (List<TemplateDTO>) getSqlMapClientTemplate().queryForList("selectCompleteTemplate", param);
	}

	@Override
	public List<TemplateDTO> retrievePagedTempaltes(String searchCode,
			String searchState, Integer orgId, Integer pageNo, Integer pageSize) {
		TemplateDTO t = new TemplateDTO();
		t.getStyle().setCode(searchCode);
		t.setState(searchState);
		t.setOrgId(orgId);
		t.setPageNo(pageNo);
		t.setPageSize(pageSize);
		return (List<TemplateDTO>) getSqlMapClientTemplate().queryForList("selectedPagedTemplates", t);
	}

	@Override
	public Integer retrieveTemplatesRecordCount(String searchCode,
			String searchState, Integer orgId) {
		TemplateDTO t = new TemplateDTO();
		t.getStyle().setCode(searchCode);
		t.setState(searchState);
		t.setOrgId(orgId);

		return (Integer) getSqlMapClientTemplate().queryForObject("selectedPagedTemplatesCount", t);
	}

}
